package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hxf.domain.Pd;
import com.ruoyi.hxf.domain.Type;
import com.ruoyi.hxf.mapper.PdMapper;
import com.ruoyi.hxf.mapper.RosterMapper;
import com.ruoyi.hxf.mapper.TypeMapper;
import com.ruoyi.hxf.service.IPdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hxf.domain.bo.PayBo;
import com.ruoyi.hxf.domain.vo.PayVo;
import com.ruoyi.hxf.domain.Pay;
import com.ruoyi.hxf.mapper.PayMapper;
import com.ruoyi.hxf.service.IPayService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 缴费(推送服务模块)Service业务层处理
 *
 * @author hxf
 * @date 2023-01-12
 */
@RequiredArgsConstructor
@Service
public class PayServiceImpl implements IPayService {

    private final PayMapper baseMapper;
    private final TypeMapper typeMapper;
    private final PdMapper pdMapper;


    /**
     * 查询缴费(推送服务模块)
     */
    @Override
    public PayVo queryById(Long payId) {
        return baseMapper.selectVoById(payId);
    }

    /**
     * 查询缴费(推送服务模块)列表
     */
    @Override
    public TableDataInfo<PayVo> queryPageList(PayBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Pay> lqw = buildQueryWrapper(bo).last("order by create_time desc");
        Page<PayVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询缴费(推送服务模块)列表
     */
    @Override
    public List<PayVo> queryList(PayBo bo) {
        LambdaQueryWrapper<Pay> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询缴费(推送服务模块)列表
     */
    @Override
    public List<Pay> List() {
        LambdaQueryWrapper<Pay> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<Pay> buildQueryWrapper(PayBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Pay> lqw = Wrappers.lambdaQuery();
        lqw.eq(Pay::getIsDelete, 0);
        lqw.like(StringUtils.isNotBlank(bo.getPayName()), Pay::getPayName, bo.getPayName());
        lqw.eq(StringUtils.isNotBlank(bo.getPayType()), Pay::getPayType, bo.getPayType());
        lqw.eq(StringUtils.isNotBlank(bo.getPushStatus()), Pay::getPushStatus, bo.getPushStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProgressStatus()), Pay::getProgressStatus, bo.getProgressStatus());
        lqw.eq(bo.getDeptId() != null, Pay::getDeptId, bo.getDeptId());
        lqw.between(params.get("beginEndTime") != null && params.get("endEndTime") != null,
                Pay::getEndTime, params.get("beginEndTime"), params.get("endEndTime"))
            .last("order by end_time desc");
        return lqw;
    }

    /**
     * 新增缴费(推送服务模块)
     */
    @Override
    public Boolean insertByBo(PayBo bo) {
        Pay add = BeanUtil.toBean(bo, Pay.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPayId(add.getPayId());
            if (bo.getPushStatus().equals("0") && bo.getDeptId() != -1) {
                List<Type> types = typeMapper.selectParentIdByDeptId(bo.getDeptId());
                for (Type type : types) {
                    if (type.getDeptId() != null) {
                        String classParents = typeMapper.selectClassParentByDeptId(Long.valueOf(type.getDeptId()));
                        if (StringUtils.isNotBlank(classParents)) {
                            for (String s : classParents.split(",")) {
                                Pd pd1 = pdMapper.selectVoByPayId(Long.valueOf(s), add.getPayId());
                                if(pd1==null){
                                    Pd pd = new Pd();
                                    pd.setPayId(String.valueOf(add.getPayId()));
                                    pd.setCreateBy(s);
                                    pdMapper.insertPd(pd);
                                }
                            }
                        }
                    }
                }
            } else if (bo.getPushStatus().equals("0") && bo.getDeptId() == -1) {
                List<Type> types = typeMapper.selectList();
                for (Type type : types) {
                    if (type.getDeptId() != null) {
                        String classParents = typeMapper.selectClassParentByDeptId(Long.valueOf(type.getDeptId()));
                        if (StringUtils.isNotBlank(classParents)) {
                            for (String s : classParents.split(",")) {
                                Pd pd1 = pdMapper.selectVoByPayId(Long.valueOf(s), add.getPayId());
                                if(pd1==null){
                                    Pd pd = new Pd();
                                    pd.setPayId(String.valueOf(add.getPayId()));
                                    pd.setCreateBy(s);
                                    pdMapper.insertPd(pd);
                                }
                            }
                        }
                    }
                }
            }
        }
        return flag;
    }

    /**
     * 修改缴费(推送服务模块)
     */
    @Override
    public Boolean updateByBo(PayBo bo) {
        Pay update = BeanUtil.toBean(bo, Pay.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Pay entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除缴费(推送服务模块)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {

        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            PayVo payVo = baseMapper.selectVoById(id);
            payVo.setIsDelete("1");
            Pay update = BeanUtil.toBean(payVo, Pay.class);
            baseMapper.updateById(update);
        }
        return true;
    }

    @Override
    public void updatePay(Pay pay) {
        baseMapper.updatePay(pay);
    }

    @Override
    public int MyUpdateByBo(PayBo bo) {
        return baseMapper.MyUpdateByBo(bo);
    }

    @Override
    public TableDataInfo<PayVo> queryPagePayList(PayBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Pay> lqw = PayBuildQueryWrapper(bo);
        Page<PayVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public void MyUpdateByVo(PayVo row) {
        baseMapper.MyUpdateByVo(row);
    }

    @Override
    public Pay selectPayByPayId(Long payId) {
        return baseMapper.selectPayByPayId(payId);
    }

    private LambdaQueryWrapper<Pay> PayBuildQueryWrapper(PayBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Pay> lqw = Wrappers.lambdaQuery();

        lqw.eq(Pay::getPushStatus, 0)
            .eq(Pay::getIsDelete, 0)
            .eq(Pay::getProgressStatus, 0)
            .eq(StringUtils.isNotBlank(bo.getPayType()), Pay::getPayType, bo.getPayType())
            .in(Pay::getDeptId, new String[]{String.valueOf(bo.getDeptId()), "-1"})
            .last("order by end_time asc");
        return lqw;
    }
}
